package com.hzit.servlet;

import com.hzit.entity.Student;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/student")
public class StudentServlet extends HttpServlet {
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Student st1 = Student.builder().sid(1001).sname("张三").sex("男").age(20).addr("上海").cid(1).build();
		Student st2 = Student.builder().sid(1002).sname("李四").sex("男").age(22).addr("西湖").cid(2).build();
		Student st3 = new Student(1003,"王五","男",22,"杭州",1);
		List<Student> studentList = new ArrayList<>();
		studentList.add(st1);
		studentList.add(st2);
		studentList.add(st3);

		String age = request.getParameter("age");
		//1. 在同一次请求范围内存入学生集合，这个集合的key：stuentList,它的值就是上面的学生集合
		request.setAttribute("studentList",studentList);
		//2. 转发发送(地址栏的地址不会发生改变，是一次请求，可以取出由request.setAttribute()定义的数据及通过请求传递的参数【像？号或表单隐藏域传参】)
		request.getRequestDispatcher("/student/list.jsp").forward(request,response);
		//3. 重写向发送(地址栏的地址会发生改变，因为是两次请求不能获取到前一次请求的所有数据。)
        // response.sendRedirect("/student/list.jsp");
		// http://localhost:9000/student ----> /WEB-INF/jsp/student/list.jsp  转发后可以得到数据

	}
}
